package com.itheima.reggin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.itheima.reggin.common.R;
import com.itheima.reggin.entity.PageDto;
import com.itheima.reggin.entity.ShoppingCart;
import com.itheima.reggin.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 获取购物车的分页数据
     * @param pageDto
     * @param session
     * @return
     */
    @GetMapping("/page")
    public R<PageDto<ShoppingCart>> ShoppingCartPage(PageDto<ShoppingCart> pageDto, HttpSession session){
        //获取用户的id
        Long userId = (Long) session.getAttribute("user");
        //使用分页构造器
        Page<ShoppingCart> page = new Page<>(pageDto.getPage(), pageDto.getPageSize());
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(userId!=null,ShoppingCart::getUserId,userId);
        shoppingCartService.page(page,lqw);
        //取出分页构造器中的数据,存放到pageDto中
        pageDto.setTotal(pageDto.getTotal());
        pageDto.setRecords(page.getRecords());
        return R.success(pageDto);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> ShoppingCartList(HttpSession session){
        //获取当前用户id
        Long userId = (Long) session.getAttribute("user");
        //通过用户id查找购物车
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(userId!=null,ShoppingCart::getUserId,userId);
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(lqw);
        return  R.success(shoppingCarts);
    }

    @PostMapping("/add")
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        //从session中获取数据
        Long userId = (Long) session.getAttribute("user");
        System.out.println(userId);
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        shoppingCart.setUserId(userId);
        shoppingCart.setCreateTime(LocalDateTime.now());
        //根据条件查询数据信息
        LambdaQueryWrapper<ShoppingCart> lqw= new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        if (dishId!=null){
            lqw.eq(ShoppingCart::getDishId,dishId);
        }else{
            lqw.eq(ShoppingCart::getSetmealId,setmealId);
        }
        ShoppingCart shoppingCarts = shoppingCartService.getOne(lqw);
        if (shoppingCarts==null){
            shoppingCart.setNumber(1);
            //将数据添加到购物车中
            shoppingCartService.save(shoppingCart);
            shoppingCarts=shoppingCart;
        }else{
            int number = shoppingCarts.getNumber();
            shoppingCarts.setNumber(number+1);
            //修改数据库数据
            shoppingCartService.updateById(shoppingCarts);
        }
        return R.success(shoppingCarts);
    }

    /**
     * 减少
     * @param shoppingCart
     * @param session
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> subShoppingCart(@RequestBody ShoppingCart shoppingCart,HttpSession session){
        //通过session获取用户ID
        Long userId = (Long) session.getAttribute("user");
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        //根据条件查询数据
        LambdaQueryWrapper<ShoppingCart> lqw= new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        if (dishId!=null){
            lqw.eq(ShoppingCart::getDishId,dishId);
        }else{
            lqw.eq(ShoppingCart::getSetmealId,setmealId);
        }
        ShoppingCart cart = shoppingCartService.getOne(lqw);
        if ((cart.getNumber()-1)==0){
            cart.setNumber(0);
            //删除购物车中的该条数据
            shoppingCartService.remove(lqw);
        }else{
            Integer number = cart.getNumber();
            cart.setNumber(number-1);
            shoppingCartService.updateById(cart);
        }
        return R.success(cart);
    }

    @DeleteMapping("/clean")
    public R<String> deleteShoppingCart(HttpSession session){
        //获取用户id
        Long userId = (Long) session.getAttribute("user");
        //清除该用户的购物车
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        boolean result = shoppingCartService.remove(lqw);
        if (result){
            return R.success();
        }
        return R.error("清空失败");
    }
}
